package uit.edu.vn.portal.portlets.mycourse;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ReadOnlyException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uit.edu.vn.portal.portlets.mycourse.helper.SakaiWebServiceHelper;
import uit.edu.vn.portal.portlets.mycourse.logic.SakaiWebServiceLogic;
import uit.edu.vn.portal.portlets.mycourse.models.CalendarEvent;
import uit.edu.vn.portal.portlets.mycourse.models.RecurrenceRule;
import uit.edu.vn.portal.portlets.mycourse.models.Site;
import uit.edu.vn.portal.portlets.mycourse.models.Time;
import uit.edu.vn.portal.portlets.mycourse.models.TimeRange;
import uit.edu.vn.portal.portlets.mycourse.support.HttpSupport;
import uit.edu.vn.portal.portlets.mycourse.utils.Constants;
import uit.edu.vn.portal.portlets.mycourse.utils.Messages;


import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class MyCourse
 */
public class MyCourse extends MVCPortlet {
	
	private final Log log = LogFactory.getLog(getClass().getName());
	
	private String adminUsername;
	private String adminPassword;
	private String loginUrl;
	private String scriptUrl;
	private String sakaiUrl;
	
	@Override
	public void init(PortletConfig config) throws PortletException {	   
		   super.init(config);
		   log.info("Start My Course Portlet init()");

		   //get params
		   adminUsername = config.getInitParameter("sakai.admin.username");
		   adminPassword = config.getInitParameter("sakai.admin.password");
		   loginUrl = config.getInitParameter("sakai.ws.login.url");
		   scriptUrl = config.getInitParameter("sakai.ws.script.url");
		   sakaiUrl = config.getInitParameter("sakai.server.url");
		   log.info("End My Course Portlet init()");	   
		}
	@Override
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		//setup the web service bean
		SakaiWebServiceLogic logic = new SakaiWebServiceLogic();
		logic.setAdminUsername(adminUsername);
		logic.setAdminPassword(adminPassword);
		logic.setLoginUrl(loginUrl);
		logic.setScriptUrl(scriptUrl);
		logic.setEid(adminUsername);

		//setup remote userId
		String remoteUserId = SakaiWebServiceHelper.getRemoteUserIdForUser(logic);
		if(StringUtils.isBlank(remoteUserId)) {
			log.error("No user info was returned from remote server.");
			return;
		}

		// get list of sites
		List<Site> sites = logic.getAllSitesForUser();
		if(sites.isEmpty()){
			log.error("No sites were returned from remote server.");
			return; 
		}
		//end get request info
		//set request info
		renderRequest.setAttribute("sites", sites);
		renderRequest.setAttribute("sakaiUrl", this.sakaiUrl);
		//end set request info
		log.info(sites.size());
		super.doView(renderRequest, renderResponse);
	}
	
	protected void dispatch(RenderRequest renderRequest, RenderResponse renderResponse, 
			String path)throws PortletException, IOException {
		renderResponse.setContentType("text/html"); 
		PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(path);
		dispatcher.include(renderRequest, renderResponse);
	}
}
